<template>
  <div class="container">
    <div class="app-container">
      <div class="top">
        <el-input
          class="search"
          v-model="queryParams.name"
          type="text"
          placeholder="请输入农产品名称"
          clearable
          @input="inputChange"
        ></el-input>
        <br />
        <el-button type="primary" class="add" @click="addProduct"
          >新增</el-button
        >
        <el-button type="danger" class="deletes" @click="deleteProducts"
          >批量删除</el-button
        >
      </div>
      <el-table
        :data="tableData"
        lazy
        stripe
        style="width: 100%"
        class="table"
        height="680"
        max-height="690"
        v-loading="loading"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          prop="name"
          label="农产品名称"
          header-align="center"
          sortable
          align="center"
          width="180"
        />
        <el-table-column
          prop="categoryName"
          label="类别"
          width="160"
          sortable
          align="center"
        />
        <el-table-column label="农产品图片" align="center" width="160">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.imageUrl"
              fit="cover"
              :preview-src-list="[scope.row.imageUrl]"
            >
              <div slot="placeholder" class="image-slot">
                加载中<span class="dot">...</span>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="描述信息" align="center" width="160">
          <template slot-scope="scope">
            <div class="block">
              <span>{{ scope.row.description }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="价格" align="center" width="160">
          <template slot-scope="scope">
            <div class="block">
              <span>{{ scope.row.price + "元/" + scope.row.unit }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="unit" label="单位" align="center" width="160" />
        <el-table-column
          prop="stock"
          label="库存数量"
          sortable
          align="center"
          width="155"
        />
        <el-table-column label="更新时间" width="160" align="center">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{
              leaveTime(scope.row.updateTime)
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="190" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pagination"
        hide-on-single-page
        layout="total, prev, pager, next"
        :total="total"
        :current-page="queryParams.page"
        :page-size="queryParams.pageSize"
        @current-change="currentChange"
      />
    </div>
    <add-product
      :showDialog.sync="showDialog"
      :categoriesData="categoriesData"
      @addProductSuccess="getProductsMsg"
      :productId="productId"
      :dialogTitle="dialogTitle"
      ref="updateProduct"
    ></add-product>
  </div>
</template>

<script>
import { getProducts, deleteProductById } from "@/api/product";
import moment from "moment";
import AddProduct from "./components/add-product.vue";
import { getCategories } from "@/api/category";

export default {
  name: "Product",
  components: {
    AddProduct,
  },
  data() {
    return {
      loading: true,
      tableData: [], // 存取商品列表数据
      categoriesData: [],
      total: 0, // 商品总条数
      //   存储查询参数
      queryParams: {
        name: null,
        page: 1, // 当前页码
        pageSize: 10, // 获取页码条数
      },
      showDialog: false, // 是否显示弹层
      productId: null,
      dialogTitle: null,
    };
  },
  created() {
    this.getProductsMsg();
    this.getCategories();
  },
  methods: {
    // 获取产品
    async getProductsMsg() {
      const { rows, total } = await getProducts(this.queryParams);
      this.tableData = rows;
      this.total = total;
      this.page = 1;
      this.loading = false;
    },
    // 切换页码
    currentChange(newpage) {
      this.queryParams.page = newpage;
      this.getProductsMsg();
    },
    // 查询商品
    searchProducts() {
      this.getProductsMsg();
    },
    // 输入框值发生变化
    inputChange() {
      // 防抖
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.queryParams.page = 1;
        this.getProductsMsg();
      }, 400);
    },
    // 转换日期格式
    leaveTime(value) {
      return moment(value).format("YYYY-MM-DD HH:mm");
    },
    // 新增
    addProduct() {
      this.dialogTitle = "新增农产品";
      this.showDialog = true;
    },
    // 获取分类
    async getCategories() {
      const { rows } = await getCategories();
      this.categoriesData = rows;
    },
    // 编辑
    handleEdit(row) {
      this.dialogTitle = "编辑农产品";
      this.showDialog = true;
      this.productId = row.id;
      this.$refs.updateProduct.editProduct(row.id);
    },
    // 删除农产品
    handleDelete(row) {
      this.$confirm("是否删除该农产品", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await deleteProductById(row.id).then((res) => {
            this.$message.success("删除农产品成功");
            this.queryParams.page = 1;
            this.queryParams.pageSize = 10;
            this.getProductsMsg();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 批量删除
    deleteProducts() {
      this.$message.success("批量删除");
    },
  },
};
</script>

<style scoped>
.search {
  width: 240px;
  margin-right: 10px;
}

.add {
  margin-top: 10px;
}
.pagination {
  margin-top: 10px;
  text-align: right;
}
</style>
